<?php
/**
 * User: Bryant Byrd
 * Date: 2/21/11
 * Time: 8:34 PM
 */
 
class Scouters_Log_Options {
    public function __construct() {
        add_action( "admin_menu", array ( $this, "add_options_menu_item" ) );
        add_action( "admin_init", array ( $this, "register_settings" ) );
    }

    public function add_options_menu_item () {
        add_menu_page( __( "General Scouters' Log Options", "slog" ),
                       __( "Scouters' Log", "slog" ),
                       "manage_options", "scouters_log_general_options",
                       array ( $this, "display_general_options_form" ),
                       SLOG_IMAGES_URL . "WorldScoutingCrest_20x20.png");
        add_submenu_page( "scouters_log_general_options",
                          __( "General Options", "slog" ),
                          __( "General Options", "slog" ),
                          "manage_options", "scouters_log_general_options",
                          array ( $this, "display_general_options_form" ) );
        add_submenu_page( "scouters_log_general_options",
                          __( "Scouters' Log Event Locations" ),
                          __( "Event Locations" ),
                          "manage_options", "scouters_log_event_location_manager",
                          array ( $this, "display_event_location_ui" ) );
         
    }
    public function register_settings () {
        register_setting( "scouters_log_options", "slog_calendar_page_id", "intval" );
    }
    public function display_general_options_form () {
        ?>
        <div class="wrap">
            <?php $this->display_option_page_header( __( "General Scouters' Log Options" ) ); ?>
            <form action="options.php" method="post" id="scouters-log-options-form">
                <?php settings_fields( "scouters_log_options" ); ?>
                <h3>
                    <label for="slog_calendar_page_id"><?php _e( "Calendar Page:" ); ?></label>
                    <?php wp_dropdown_pages( array ( 'selected' => get_option( 'slog_calendar_page_id' ),
                                                    'name' => 'slog_calendar_page_id' ) ); ?>
                </h3>
                <p><input type="submit" name="submit" value="Save Settings" class="button" /></p>
            </form>
        </div>
        <?php
    }
    public function display_event_location_ui () {
        if ( isset ( $_POST [ "action" ] ) )
            process_action ( $_POST ( "action" ) );
        elseif ( isset ( $_POST [ "submit" ] ) && "Add New" == $_POST [ "submit" ] )
            process_action ( "new" );
        else
            $this->display_event_location_manager();
    }
    private function process_action ( $action ) {
        switch ( $action ) {
            case "new":
                $this->display_event_location_editor ( "new" );
                break;
            case "edit":
                $this->display_event_location_editor ( $_POST [ "loc_id" ] );
                break;
        }
    }
    private function display_event_location_editor ( $location_id ) {
        require ( SLOG_UI_PATH . "forms/Event_Location_Editor.php" );
    }
    public function display_event_location_manager () {
        require ( SLOG_UI_PATH . "forms/Event_Location_Manager.php" );
    }
    private function display_option_page_header ( $option_page_title ) {
        ?>
        <h2 id="slog_option_title" style="margin: 10px 0px 0px 0px; padding: 0px 0px 0px 56px; height: 45px; background: url(<?php
            echo SLOG_IMAGES_URL . "WorldScoutingCrest_45x45.png";
            ?>) no-repeat"><?php echo $option_page_title ?></h2>
        <?php
    }
}
